package project1.service;

import project1.Main;
import project1.dao.BookDao;
import project1.dao.BookDaoImpl;
import project1.dao.ShoppingCartDao;
import project1.dao.ShoppingCartDaoImpl;
import project1.entity.Book;

public class ShopServiceImpl implements ShopService {

    private static final BookDao bookDao = new BookDaoImpl();
    private static final ShoppingCartDao shoppingCartDao = new ShoppingCartDaoImpl();

    @Override
    public void addToCart() {
        System.out.println("请输入要购买书籍编号：");
        String bid = Main.scanner.next();
        Book book = bookDao.query(bid);
        if (book == null) {
            System.out.println("书籍不存在！");
            return;
        }
        System.out.println("请输入购买数：");
        int count = Main.scanner.nextInt();
        if (count <= 0) {
            System.out.println("购买数非法！");
            return;
        } else if (count > book.getNumber()) {
            System.out.println("购买数非法！");
            return;
        }
        shoppingCartDao.insert(book, count); // 加入购物车
        book.setNumber(book.getNumber() - count); // 库存减少
        System.out.println("加购成功");
    }

    @Override
    public void removeFromCart() {
        System.out.println("请输入要清除购物车中书籍编号：");
        String bid = Main.scanner.next();
        Book book = bookDao.query(bid);
        if (book == null) {
            System.out.println("书籍不存在！");
            return;
        }
        int count = shoppingCartDao.queryCount(book);// 查询书籍购买数
        shoppingCartDao.delete(book); // 清除购物车中书籍
        book.setNumber(book.getNumber() + count); // 库存增加
        System.out.println("取消加购成功");
    }

    @Override
    public void showCart() {
        System.out.println(shoppingCartDao.query());
    }
}
